package com.attendance.result;

import com.attendance.pojo.Menu;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

/**
 * 初始化菜单
 *
 * @author 今朝
 * @date 2021/03/16
 */
@Data
public class InitMenu {
    HomeInfo homeInfo;
    LogoInfo logoInfo;
    List<SystemMenu> menuInfo;

    public InitMenu(String homeTitle, String homeHref, String logoTitle, String logoImage, String logoHref, List<Menu> menus) {
        this.homeInfo = new HomeInfo(homeTitle, homeHref);
        this.logoInfo = new LogoInfo(logoTitle, logoImage, logoHref);
        List<SystemMenu> menuInfo = new ArrayList<>();
        for (Menu menu : menus) {
            SystemMenu systemMenu = copy(menu);
            menuToSysMenu(menu, systemMenu);
            menuInfo.add(systemMenu);
        }
        this.menuInfo = menuInfo;
    }

    public void menuToSysMenu(Menu menu, SystemMenu systemMenu) {
        if (menu.getChildren() != null) {
            List<SystemMenu> systemMenus = new ArrayList<>();
            systemMenu.setChild(systemMenus);
            for (Menu child : menu.getChildren()) {
                SystemMenu copy = copy(child);
                menuToSysMenu(child, copy);
                systemMenus.add(copy);
            }
        }
    }

    public SystemMenu copy(Menu menu) {
        SystemMenu systemMenu = new SystemMenu();
        systemMenu.setTitle(menu.getTitle());
        systemMenu.setIcon("fa " + menu.getIcon());
        systemMenu.setHref(menu.getUrl());
        systemMenu.setTarget("_self");
        return systemMenu;
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class HomeInfo {
        String title;
        String href;
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class LogoInfo {
        String title;
        String image;
        String href;
    }

    @Data
    public class SystemMenu {
        String title;
        String icon;
        String href;
        String target;
        List<SystemMenu> child;
    }
}
